package com.zsc.wxm.ysh.anjo.sms.service.impl;

import com.alibaba.fastjson.JSON;
import com.zsc.wxm.ysh.anjo.result.CodeEnum;
import com.zsc.wxm.ysh.anjo.result.Result;
import com.zsc.wxm.ysh.anjo.service.base.exception.AnjoException;
import com.zsc.wxm.ysh.anjo.service.base.model.member.bo.MemberBO;
import com.zsc.wxm.ysh.anjo.service.base.model.member.dto.MemberInfoDTO;
import com.zsc.wxm.ysh.anjo.service.base.model.member.qo.MemberQuery;
import com.zsc.wxm.ysh.anjo.sms.feign.MemberService;
import com.zsc.wxm.ysh.anjo.sms.model.MailModel;
import com.zsc.wxm.ysh.anjo.sms.service.MailService;
import com.zsc.wxm.ysh.anjo.utils.Md5Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 描述：
 *
 * @author playmaker
 * @since 2021/2/9 0:50
 */
@Service
@Slf4j
public class MailServiceImpl implements MailService {

    @Value("${spring.mail.from}")
    private String from;

    @Resource
    private JavaMailSender javaMailSender;

    @Resource
    private MemberService memberService;

    @Override
    public void sendBackPasswordEmail(String email) {
        Result result = memberService.getOne(MemberQuery.builder().email(email).limit(1L).build());
        log.info("获取的结果，{}", result.getData().get("memberBo"));
        if (result.getData() == null) {
            throw new AnjoException(CodeEnum.EMAIL_IS_NOT_EXIT);
        }
        String dataString = JSON.toJSONString(result.getData().get("memberBo"));
        MemberBO memberBo = JSON.parseObject(dataString, MemberBO.class);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
        try {
            //生成6位随机密码
            String newPassword = RandomStringUtils.randomNumeric(6);
            MailModel mailModel = setBackPasswordEmailModel(email, newPassword);
            memberBo.setPassword(Md5Util.md5(newPassword));
            MemberInfoDTO memberInfoDto = MemberInfoDTO.builder().build();
            BeanUtils.copyProperties(memberBo,memberInfoDto);
            //修改密码
            memberService.updateMemberInfo(memberInfoDto);
            helper.setFrom(from);
            helper.setTo(mailModel.getTo());
            helper.setSubject(mailModel.getSubject());
            helper.setText(mailModel.getText());
            javaMailSender.send(mimeMessage);

            log.info("邮件发送成功");
        } catch (Exception e) {
           log.error("sendBackPasswordEmail邮件发送失败", e);
           throw new AnjoException(CodeEnum.SEND_EMAIL_ERROR);
        }
    }

    @Override
    public void sendViewRequest(String code, String mail) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
        MailModel mailModel = setViewRequestEmailModel(code, mail);
        try {
            helper.setFrom(from);
            helper.setTo(mailModel.getTo());
            helper.setSubject(mailModel.getSubject());
            helper.setText(mailModel.getText());
            javaMailSender.send(mimeMessage);
            log.info("邮件发送成功");
        } catch (MessagingException e) {
            log.error("sendViewRequest邮件发送失败，失败信息：{}", e.getMessage());
            throw new AnjoException(CodeEnum.SEND_EMAIL_ERROR);
        }
    }

    private MailModel setBackPasswordEmailModel(String email, String newPassword) {
        MailModel mailModel = new MailModel();
        mailModel.setTo(email);
        mailModel.setSubject("【安家时代-密码重置】");
        mailModel.setText("您好！您的密码已重新设置为：" + newPassword + "，请登录后重置密码！");
        return mailModel;
    }

    private MailModel setViewRequestEmailModel(String code, String email) {
        MailModel mailModel = new MailModel();
        mailModel.setTo(email);
        mailModel.setSubject("【安家时代-看房请求】");
        mailModel.setText("您好！您的上传的房源被租客看中啦！房源编号为：" + code + "，请尽快在小程序中确认！");
        return mailModel;
    }
}
